package com.pwc.testing;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pwc.common.Util.ILog;
import com.pwc.common.Util.Time;

@Component
public class LogUtil {

	private String logPath;
	private String path;

	@Autowired
	private ILog log;

	

	public LogUtil() {

	}

	public String getLogPath() {
		return logPath;
	}

	public void setLogPath(String logPath) {
		this.logPath = logPath;
		
	}

	public ILog getLog() {
		return log;
	}

	public void setLog(ILog log) {
		this.log = log;
	}

	public void addLog(String msg) {
		log.addLog(msg);
		save();
	}

	public void setLogName(String logName) {
		log.setLogName(logName);
		File logPathFile = new File(logPath);
		path = logPathFile.getAbsolutePath() + File.separator + log.getLogName() + "_"
				+ Time.getDateTime("yyyyMMddHHmmss") + ".log";
	}

	public void save() {

		File tempFile = new File(path);

		List<String> logs = log.getLogs();

		String result = StringUtils.join(logs, '\n');

		try {
			FileUtils.writeStringToFile(tempFile, result);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
